package com.yf.modules.system.controller;

import com.yf.Utils.PageUtil;
import com.yf.domain.security.Job;
import com.yf.model.query.JobQueryCriteria;
import com.yf.modules.system.service.api.JobService;
import com.yf.response.ResponseBody;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Set;

/**
 * @author chentianwei
 * @date 2024-10-29 13:14
 * @description JobRestful
 */
@RestController
@RequestMapping("/api/job")
@Slf4j
@Tag(name = "Job接口", description = "岗位Api")
public class JobController {

    private final JobService jobService;

    public JobController(JobService jobService) {
        this.jobService = jobService;
    }

    @GetMapping("")
    @Operation(summary = "获取岗位信息", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<ResponseBody<Object>> queryJob(JobQueryCriteria criteria) {
        List<Job> jobList = jobService.queryAll(criteria);
        return ResponseEntity.ok(ResponseBody.success(PageUtil.toPage(jobList, jobList.size())));
    }

    @PostMapping("/add")
    @Operation(summary = "添加岗位信息", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<ResponseBody<String>> addJob(@RequestBody Job job) {
        jobService.create(job);
        return ResponseEntity.ok(ResponseBody.success("添加成功"));
    }

    @PutMapping("/edit")
    @Operation(summary = "修改岗位信息", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<ResponseBody<String>> editJob(@RequestBody Job job) {
        jobService.update(job);
        return ResponseEntity.ok(ResponseBody.success("修改成功"));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除岗位信息", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<ResponseBody<String>> deleteJobs(@RequestBody Set<Long> ids) {
        jobService.delete(ids);
        return ResponseEntity.ok(ResponseBody.success("删除成功"));
    }

    @GetMapping("/download")
    @Operation(summary = "下载岗位信息", security = @SecurityRequirement(name = "authScheme"))
    public void downloadJob(JobQueryCriteria criteria, HttpServletResponse response) {
        try {
            jobService.download(criteria, response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


}
